package des.wangku.operate.standard.testing;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class Testing1 {
	 public static String getHTML(String targetUrl){
		 
	        //1.确定url
	        //String url = "https://picjumbo.com/free-stock-photos/love/";
	        String url = targetUrl;
	        //2.设置请求方式
	        HttpGet hg = new HttpGet(url);
	        hg.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
	        //3.创建发送对象
	        CloseableHttpClient httpClient = HttpClients.createDefault();
	        //4.发送请求,获取响应对象
	        //CloseableHttpResponse里面封装了响应头响应体响应行
	        CloseableHttpResponse response = null;
	        try {
	            response = httpClient.execute(hg);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	 
	 
	        String html="";
	        //5.解析响应对象
	        if (response.getStatusLine().getStatusCode() == 200) {
	            //获取响应体
	            HttpEntity entity = response.getEntity();
	            try {
	                //解码响应体（根据网页的编码解码）
	                html = EntityUtils.toString(entity,"gbk");
	                Header head=entity.getContentEncoding();
	                if(head!=null) {

		                HeaderElement[] arr=head.getElements();
		                for(HeaderElement f:arr) {
		                	System.out.println(f.getName()+"="+f.getValue());
		                }
	                }
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        try {
	            //关闭发送对象
	            httpClient.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	 
	        return html;
	    }
	public static void main(String[] args) {
		String url="http://www.zg114zs.com/index.html";
		
		getHTML(url);
		//System.out.println();
	}
}
